'''
Created on 2010-09-04

'''
import re

class lcboItem(object):
    ''' An lcbo item.
    
    This class holds information about a single lcbo item. An lcbo item is 
    anything that can be bought from the lcbo i.e beer or whisky.
    '''
            
    def __init__(self,title,quantity,price,category,percentage,geography,brewer):
        ''' Default constructor.
        @param title: The title of the item (i.e pabst blue ribbon)
        @type title: string
        
        @param quantity: The quantity involved i.e '3x355 mL cans'
        @type quantity: string
        
        @param category: The category of booze i.e beer/spirits/wine
        @type category: string
        
        @param percentage: The strength of the item in percent i.e 40%
        @type percentage: string
        
        @param geography: The geographic location of where this brew originates
        from.
        @type geography: string
        
        @param brewer: The company that manufactures this item
        @type brewer: string
         
        '''
        self.title = title #:The title of the item (i.e pabst blue ribbon) 
        
        
        #'750 mL bottle' becomes ['750 ', 'bottle']
        if quantity is not None:
            volume,type = quantity.split('mL')
        else:
            volume = None
            type = None
            
        self.determineQuantity(volume)
        self.type = type #:The type of container (Can,Bottle,TetraPack etc).
        
        self.price = price #:The price for this item
        self.category = category #:The category of booze i.e beer/spirits/wine
        self.percentage = percentage 
        '''The strength of the item in percent i.e 40%'''
        self.geography = geography
        '''The geographic location of where this brew originates'''
        self.brewer = brewer #:The company that manufactures this item
        self.cleanup()
    
    def determineQuantity(self,quantity):
        '''Parse the quantity string and convert into an amount and size.
        Take this string and determine the number of bottles as well 
        as their sizes.
        
        @param quantity: A string typically in the form of '750 mL bottle'
        or '3x355 mL Cans', etc.
        @type quantity: string
        '''
        
        if quantity == None:
            self.amount = None #:The number of cans/bottles 
            self.size = None #The size of each can/bottle
            return
        
        #split 6x355 into amount = 6 size = 355
        #or just 355 into amount = 1 size = 355
        index = quantity.find('x') 
        if index != -1:
            amount = int(quantity[:index])
            size = int(quantity[index+1:])
        else:
            amount = 1
            size = int(quantity.strip())
        
        self.amount = amount
        self.size = size
        
    def cleanup(self):
        '''Strip whitespace from saved fields (title, type, category, etc...).
        Also convert the price and percentage fields into floats.
        '''
        if self.title is not None: self.title = self.title.strip()
        
        #Make this consistent with the beer database
        if self.type is not None : self.type = self.type.strip()
        if self.type == 'can': self.type = 'Cans'
        elif self.type == 'bottle': self.type = 'Bottle'
        
        if self.category is not None: self.category = self.category.strip()
        if self.geography is not None: self.geography = self.geography.strip()
        if self.brewer is not None: self.brewer = self.brewer.strip()
        if self.price is not None: self.price = float(self.price)
        if self.percentage is not None: self.percentage = float(self.percentage)
        self.unicodeConvert()
        
    def unicodeConvert(self):
        '''Convert the saved fields (title, type, category, etc...)
        into unicode, ignoring any errors'''
        
        vars = self.__dict__
        for attrs in vars:
            if (attrs != 'amount' and attrs != 'size' and \
                attrs != 'price' and attrs != 'percentage'):
                vars[attrs] = unicode(vars[attrs], errors='ignore')
